#import "GameViewController.h"
#import <math.h>
#import "HomeViewController.h"
#import "Wolf.h"
#import "ChildWolf.h"
#import "OldWolf.h"
#import "ProgressView.h"

@interface GameViewController ()

@end

@implementation GameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    gameTime = 20.0f;
    logoW = SCREEN_WIDTH ;
    logoH = SCREEN_HEIGHT ;
    gameView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"game_bg"]];//初始化主界面背景图
    gameView.frame = SCREEN_BOUND;
    [self.view addSubview:gameView];
    gameView.userInteractionEnabled = YES;
    scoreLabel1 = [UILabel new];//初始化主界面分数板
    scoreLabel1.frame = CGRectMake(logoW*18/100, logoH*1/1000, 70, 70);
    scoreLabel1.font = GAMEFONT;
    scoreLabel1.textColor = [UIColor orangeColor];
    [scoreLabel1 setText:@"0"];
    [gameView addSubview:scoreLabel1];
    //初始化四个狼,并且设置为可交互
    wolf1=[[OldWolf alloc] initOldWolf];
    wolf2=[[OldWolf alloc] initOldWolf];
    wolf3=[[OldWolf alloc] initOldWolf];
    wolf4=[[OldWolf alloc] initOldWolf];
    wolf5=[[ChildWolf alloc] initChildWolf];
    wolf6=[[ChildWolf alloc] initChildWolf];
    wolf7=[[ChildWolf alloc] initChildWolf];
    wolf8=[[ChildWolf alloc] initChildWolf];
    
    //imageview点击事件
    UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress1:)];
    UITapGestureRecognizer *singleTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress2:)];
    UITapGestureRecognizer *singleTap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress3:)];
    UITapGestureRecognizer *singleTap4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress4:)];
    UITapGestureRecognizer *singleTap5 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress5:)];
    UITapGestureRecognizer *singleTap6 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress6:)];
    UITapGestureRecognizer *singleTap7 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress7:)];
    UITapGestureRecognizer *singleTap8 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress8:)];
    [wolf1 addGestureRecognizer:singleTap1];
    [wolf2 addGestureRecognizer:singleTap2];
    [wolf3 addGestureRecognizer:singleTap3];
    [wolf4 addGestureRecognizer:singleTap4];
    [wolf5 addGestureRecognizer:singleTap5];
    [wolf6 addGestureRecognizer:singleTap6];
    [wolf7 addGestureRecognizer:singleTap7];
    [wolf8 addGestureRecognizer:singleTap8];
    
    [self startGame];
}

-(void)startGame{
    Score = 0 ;//初始化分数为0;
    
    //进度条
    gameProgressView = [[ProgressView alloc] initWithFrame:CGRectMake(logoW*19/100, logoH*135/1000, logoW*57/100, logoH*38/1000)];
    gameProgressView.decrease = (logoW*57/100)/PROGRESS_SEPARATE;
    [gameProgressView startTimer];
    [self.view addSubview:gameProgressView];
    
    //创建狼间隔定时器，每隔1秒出现一次狼
    appearTimer = [NSTimer scheduledTimerWithTimeInterval:WORF_TIME*1/2 target:self selector:@selector(appearWolfs) userInfo:nil repeats:YES];
    
    //创建总游戏时间定时器
    progressTimer = [NSTimer scheduledTimerWithTimeInterval:GAME_TIME target:self selector:@selector(closeGame) userInfo:nil repeats:NO];
}

-(void)appearWolfs{
    //设置狼标识随机数
    int wolfName1 = arc4random()%8;
    NSValue *nv1= [self getRandomPointNSValue];
    if ([nv1 CGRectValue].origin.x != [nv2 CGRectValue].origin.x && [nv1 CGRectValue].origin.y != [nv2 CGRectValue].origin.y) {
        if (wolfName1 != wolfName2) {
            switch (wolfName1) {
                    case 0:
                    wolf1.frame = [self setRandomPoint:nv1].frame;
                    [wolf1 appear];
                    wolf1.userInteractionEnabled=YES;
                    wolfTimer = [NSTimer scheduledTimerWithTimeInterval:WORF_TIME*9/10 target:self selector:@selector(setUnEnable1) userInfo:nil repeats:NO];
                    [self.view addSubview:wolf1];
                    break;
                case 1:
                    wolf5.frame = [self setRandomPoint:nv1].frame;
                    [wolf5 appear];
                    wolf5.userInteractionEnabled=YES;
                    wolfTimer = [NSTimer scheduledTimerWithTimeInterval:WORF_TIME*9/10 target:self selector:@selector(setUnEnable5) userInfo:nil repeats:NO];
                    [self.view addSubview:wolf5];
                    break;
                case 2:
                    wolf2.frame = [self setRandomPoint:nv1].frame;
                    [wolf2 appear];
                    wolf2.userInteractionEnabled=YES;
                    wolfTimer = [NSTimer scheduledTimerWithTimeInterval:WORF_TIME*9/10 target:self selector:@selector(setUnEnable2) userInfo:nil repeats:NO];
                    [self.view addSubview:wolf2];
                    break;
                
                case 3:
                    wolf6.frame = [self setRandomPoint:nv1].frame;
                    [wolf6 appear];
                    wolf6.userInteractionEnabled=YES;
                    wolfTimer = [NSTimer scheduledTimerWithTimeInterval:WORF_TIME*9/10 target:self selector:@selector (setUnEnable6) userInfo:nil repeats:NO];
                    [self.view addSubview:wolf6];
                    break;
                case 4:
                    wolf3.frame = [self setRandomPoint:nv1].frame;
                    [wolf3 appear];
                    wolf3.userInteractionEnabled=YES;
                    wolfTimer = [NSTimer scheduledTimerWithTimeInterval:WORF_TIME*9/10 target:self selector:@selector (setUnEnable3) userInfo:nil repeats:NO];
                    [self.view addSubview:wolf3];
                    break;
                case 5:
                    wolf7.frame = [self setRandomPoint:nv1].frame;
                    [wolf7 appear];
                    wolf7.userInteractionEnabled=YES;
                    wolfTimer = [NSTimer scheduledTimerWithTimeInterval:WORF_TIME*9/10 target:self selector:@selector (setUnEnable7) userInfo:nil repeats:NO];
                    [self.view addSubview:wolf7];
                    break;
                case 6:
                    wolf4.frame = [self setRandomPoint:nv1].frame;
                    [wolf4 appear];
                    wolf4.userInteractionEnabled=YES;
                    wolfTimer = [NSTimer scheduledTimerWithTimeInterval:WORF_TIME*9/10 target:self selector:@selector (setUnEnable4) userInfo:nil repeats:NO];
                    [self.view addSubview:wolf4];
                    break;
                case 7:
                    wolf8.frame = [self setRandomPoint:nv1].frame;
                    [wolf8 appear];
                    wolf8.userInteractionEnabled=YES;
                    wolfTimer = [NSTimer scheduledTimerWithTimeInterval:WORF_TIME*9/10 target:self selector:@selector (setUnEnable8) userInfo:nil repeats:NO];
                    [self.view addSubview:wolf8];
                    break;
                default:
                    break;
            }
            wolfName2 = wolfName1;
        }
        nv2 = nv1 ;
    }
}

-(void)closeGame{
    [wolf1 stopAnimating];
    [wolf2 stopAnimating];
    [wolf3 stopAnimating];
    [wolf4 stopAnimating];
    [wolf5 stopAnimating];
    [wolf6 stopAnimating];
    [wolf7 stopAnimating];
    [wolf8 stopAnimating];
    wolf1.userInteractionEnabled = NO;
    wolf2.userInteractionEnabled = NO;
    wolf3.userInteractionEnabled = NO;
    wolf4.userInteractionEnabled = NO;
    wolf5.userInteractionEnabled = NO;
    wolf6.userInteractionEnabled = NO;
    wolf7.userInteractionEnabled = NO;
    wolf8.userInteractionEnabled = NO;
    [appearTimer invalidate];
    [progressTimer invalidate];
    [wolfTimer invalidate];
    gameProgressView = nil;
    appearTimer = nil;
    overGame = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [overGame setImage:[UIImage imageNamed:@"score"]];
    [gameView addSubview:overGame];
    overGame.userInteractionEnabled = YES;//允许UIImageview用户交互
    
    UILabel *scoreLabel2 = [UILabel new];
    scoreLabel2.frame = CGRectMake(logoW*52/100,  logoH*41/100, 80, 80);
    scoreLabel2.font = GAMEFONT;
    NSString *s2 = [NSString stringWithFormat:@"%d",Score];
    [scoreLabel2 setText:s2];
    [overGame addSubview:scoreLabel2];
    
    UIButton *overBtn1 = [UIButton new];
    overBtn1.frame = CGRectMake(logoW*35/100,  logoH*52/100, logoW*1/3, 40);
    [overBtn1 setTitle:@"再来一局" forState:0];
    overBtn1.backgroundColor = GRAY_COLOR;
    [overBtn1 addTarget:self action:@selector(restartGame) forControlEvents:UIControlEventTouchUpInside];
    [overGame addSubview:overBtn1];
    
    UIButton *overBtn2 = [UIButton new];
    overBtn2.frame = CGRectMake(logoW*35/100,  logoH*60/100, logoW*1/3, 40);
    [overBtn2 setTitle:@"返回主页" forState:0];
    overBtn2.backgroundColor = GRAY_COLOR;
    [overBtn2 addTarget:self action:@selector(returnHome) forControlEvents:UIControlEventTouchUpInside];
    [overGame addSubview:overBtn2];
}

-(void)restartGame{
    [overGame removeFromSuperview];//移除视图
    overGame = nil;
    [scoreLabel1 setText:@"0"];
    [self startGame];
}

-(void)returnHome{
    //回到主页
    [self dismissViewControllerAnimated:NO completion:nil];
}

/**
 *  利用随机数来产生随机地址的方法
 *
 *  @param nv 随机数
 *
 *  @return 随机地址
 */
-(UIView *)setRandomPoint:(NSValue *)nv{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake([nv CGRectValue].origin.x*logoW/100, [nv CGRectValue].origin.y*logoH/100, [nv CGRectValue].size.width*logoW/100, [nv CGRectValue].size.height*logoW/100)];
    return view;
}
/**
 *  选择随机相对地址的函数
 *
 *  @return 返回一个存有相对地址的NSValue对象
 */
-(NSValue *)getRandomPointNSValue{
    //存放九个洞的相对位置的坐标
    NSValue *v1=[NSValue valueWithCGRect:CGRectMake(32, 28, 30, 30)];
    NSValue *v2=[NSValue valueWithCGRect:CGRectMake(60, 33, 30, 30)];
    NSValue *v3=[NSValue valueWithCGRect:CGRectMake(8, 37, 30, 30)];
    NSValue *v4=[NSValue valueWithCGRect:CGRectMake(33, 44, 30, 30)];
    NSValue *v5=[NSValue valueWithCGRect:CGRectMake(63, 48, 30, 30)];
    NSValue *v6=[NSValue valueWithCGRect:CGRectMake(7, 50, 30, 30)];
    NSValue *v7=[NSValue valueWithCGRect:CGRectMake(38, 61, 30, 30)];
    NSValue *v8=[NSValue valueWithCGRect:CGRectMake(10, 65, 30, 30)];
    NSValue *v9=[NSValue valueWithCGRect:CGRectMake(65, 66, 30, 30)];
    NSArray *pointArr = [NSArray arrayWithObjects:v1,v2,v3,v4,v5,v6,v7,v8,v9, nil];
    //建立随机数,随机选取九个坐标中的一个坐标
    int index = arc4random()%9;
    return pointArr[index];
}

-(void)buttonpress1:(UITapGestureRecognizer *)gestureRecognizer{
    //如果是狼出来
    if ([wolf1 isAnimating]) {
        //停止狼出来动画
        [wolf1 stopAnimating];
        //w1关闭点击功能
        wolf1.userInteractionEnabled = NO;
        //播放被打动画
        [wolf1 beHit];
        //计分
        Score=Score+wolf1.ASScore;
        //主界面计分板刷新分数
        NSString *s1 = [NSString stringWithFormat:@"%d",Score];
        [scoreLabel1 setText:s1];
    }
}

-(void)buttonpress2:(UITapGestureRecognizer *)gestureRecognizer{
    //如果是狼出来
    if ([wolf2 isAnimating]) {
        //停止狼出来动画
        [wolf2 stopAnimating];
        //w2关闭点击功能
        wolf2.userInteractionEnabled = NO;
        //播放被打动画
        [wolf2 beHit];
        //计分
        Score=Score+wolf2.ASScore;
        //主界面计分板刷新分数
        NSString *s1 = [NSString stringWithFormat:@"%d",Score];
        [scoreLabel1 setText:s1];
    }
}

-(void)buttonpress3:(UITapGestureRecognizer *)gestureRecognizer{
    if ([wolf3 isAnimating]) {
        //停止狼出来动画
        [wolf3 stopAnimating];
        //w3关闭点击功能
        wolf3.userInteractionEnabled = NO;
        //播放被打动画
        [wolf3 beHit];
        //计分
        Score=Score+wolf3.ASScore;
        //主界面计分板刷新分数
        NSString *s1 = [NSString stringWithFormat:@"%d",Score];
        [scoreLabel1 setText:s1];
    }
}

-(void)buttonpress4:(UITapGestureRecognizer *)gestureRecognizer{
    if ([wolf4 isAnimating]) {
        //停止狼出来动画
        [wolf4 stopAnimating];
        //w4关闭点击功能
        wolf4.userInteractionEnabled = NO;
        //播放被打动画
        [wolf4 beHit];
        //计分
        Score=Score+wolf4.ASScore;
        //主界面计分板刷新分数
        NSString *s1 = [NSString stringWithFormat:@"%d",Score];
        [scoreLabel1 setText:s1];
    }
}

-(void)buttonpress5:(UITapGestureRecognizer *)gestureRecognizer{
    if ([wolf5 isAnimating]) {
        //停止狼出来动画
        [wolf5 stopAnimating];
        //w4关闭点击功能
        wolf5.userInteractionEnabled = NO;
        //播放被打动画
        [wolf5 beHit];
        //计分
        Score=Score+wolf5.ASScore;
        //主界面计分板刷新分数
        NSString *s1 = [NSString stringWithFormat:@"%d",Score];
        [scoreLabel1 setText:s1];
    }
}

-(void)buttonpress6:(UITapGestureRecognizer *)gestureRecognizer{
    if ([wolf6 isAnimating]) {
        //停止狼出来动画
        [wolf6 stopAnimating];
        //w4关闭点击功能
        wolf6.userInteractionEnabled = NO;
        //播放被打动画
        [wolf6 beHit];
        //计分
        Score=Score+wolf6.ASScore;
        //主界面计分板刷新分数
        NSString *s1 = [NSString stringWithFormat:@"%d",Score];
        [scoreLabel1 setText:s1];
    }
}

-(void)buttonpress7:(UITapGestureRecognizer *)gestureRecognizer{
    if ([wolf7 isAnimating]) {
        //停止狼出来动画
        [wolf7 stopAnimating];
        //w4关闭点击功能
        wolf7.userInteractionEnabled = NO;
        //播放被打动画
        [wolf7 beHit];
        //计分
        Score=Score+wolf7.ASScore;
        //主界面计分板刷新分数
        NSString *s1 = [NSString stringWithFormat:@"%d",Score];
        [scoreLabel1 setText:s1];
    }
}

-(void)buttonpress8:(UITapGestureRecognizer *)gestureRecognizer{
    if ([wolf8 isAnimating]) {
        //停止狼出来动画
        [wolf8 stopAnimating];
        //w4关闭点击功能
        wolf8.userInteractionEnabled = NO;
        //播放被打动画
        [wolf8 beHit];
        //计分
        Score=Score+wolf8.ASScore;
        //主界面计分板刷新分数
        NSString *s1 = [NSString stringWithFormat:@"%d",Score];
        [scoreLabel1 setText:s1];
    }
}

//关闭wolf1点击功能
-(void)setUnEnable1{
    wolf1.userInteractionEnabled = NO;
}

//关闭wolf2点击功能
-(void)setUnEnable2{
    wolf2.userInteractionEnabled = NO;
}

//关闭wolf3点击功能
-(void)setUnEnable3{
    wolf3.userInteractionEnabled = NO;
}

//关闭wolf4点击功能
-(void)setUnEnable4{
    wolf4.userInteractionEnabled = NO;
}

//关闭wolf4点击功能
-(void)setUnEnable5{
    wolf5.userInteractionEnabled = NO;
}

//关闭wolf4点击功能
-(void)setUnEnable6{
    wolf6.userInteractionEnabled = NO;
}

//关闭wolf4点击功能
-(void)setUnEnable7{
    wolf7.userInteractionEnabled = NO;
}

//关闭wolf4点击功能
-(void)setUnEnable8{
    wolf8.userInteractionEnabled = NO;
}

@end
